package org.ucas.faker.web.utils.tech;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class PatentFeaturize{
	private PatentFeaturize(){
		
	}
	private static PatentFeaturize3 pf3;
	private static PatentFeaturize singleton = null;
	private static 	PatentFeaturizeRegularRule pfRRSingleton = null;
    private static KeywordTree kt = null;
	
	public static PatentFeaturize getInstance(){
		if(singleton==null){
			pf3 = PatentFeaturize3.getInstance();
			pfRRSingleton = PatentFeaturizeRegularRule.getInstance();
			 kt = new KeywordTree();
			singleton = new PatentFeaturize();
		}
		return singleton;
	}
	
	public Map<String,String> featurizeAbstract(String abstra){
		//替换全角；，．等标点
		abstra = Preprocess.replaceSplit(abstra);
		
		//获取分词及标注
		String fenci_res = new CommUtils().postTopic(abstra);
		
		//System.out.println(fenci_res+"\n");
		//Map<String,List<String> > map = PatentFeaturize3.getInstance().featurize(s+"  "+res);
        //System.out.println(lineTxt);
        List<String> lkeyword = new ArrayList<>();
        String [] splits = fenci_res.split("##");
        String keywords = splits[0];
        String sent_prop = splits[1];
        for(String s : splits[0].split("\\|")){
        	lkeyword.add(s);
        }        
		
        //获取技术分支与功效
        //使用获得的关键词进行匹配
        Set<String> bra = new HashSet<>();
        Set<String> eff = new HashSet<>();
        kt.classify(abstra, bra, eff,lkeyword);
        
        //调用基于规则的分配算法
		List<String> list =  pf3.featurizeString(abstra,lkeyword, sent_prop);
		
		//save the results
		Map<String,String> res= new HashMap<>();
		
		//将KeywordTree与featurizeString结果合并
		String branch = list.get(0);
		String effect = list.get(1);
		for(String s:bra) branch = s+"#"+branch;
		for(String s:eff) effect = s+"#"+effect;
		
		res.put("branch", branch);
		res.put("effect", effect);
		
		return res;
	}
	
	

	public Map<String,String> featurizeAbstractWithRegularRule(String abstra){
		//替换全角；，．等标点
		abstra = Preprocess.replaceSplit(abstra);
		
		//获取分词及标注
		String fenci_res = new CommUtils().postTopic(abstra);
		
		System.out.println(fenci_res.split("##")[0]+"\n");
		//Map<String,List<String> > map = PatentFeaturize3.getInstance().featurize(s+"  "+res);
        //System.out.println(lineTxt);
        List<String> lkeyword = new ArrayList<>();
        String [] splits = fenci_res.split("##");
        String keywords = splits[0];
        String sent_prop = splits[1];
        for(String s : splits[0].split("\\|")){
        	lkeyword.add(s);
        }        
		
        //获取技术分支与功效
        //使用获得的关键词进行匹配
        Set<String> bra = new HashSet<>();
        Set<String> eff = new HashSet<>();
        kt.classify(abstra, bra, eff,lkeyword);
        
        //调用基于规则的分配算法
		List<String> list =  
				pfRRSingleton.featurizeString(abstra,lkeyword, sent_prop);
		
		//save the results
		Map<String,String> res= new HashMap<>();
		
		//将KeywordTree与featurizeString结果合并
		String branch = list.get(0);
		String effect = list.get(1);
		for(String s:bra) branch = s+"#"+branch;
		for(String s:eff) effect = s+"#"+effect;
		
		res.put("branch", branch);
		res.put("effect", effect);
		
		return res;
	}
	
	
	private void TestDirectlyFromFile(String filepos){
		int order = 1;
		try {
        File file=new File(filepos);
        if(file.isFile() && file.exists()){ //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
            new FileInputStream(file));//考虑到编码格式
            BufferedReader featureReader =  new BufferedReader(read);
            String lineTxt = null;
            while((lineTxt = featureReader.readLine()) != null){
                //System.out.println(lineTxt);
            	System.out.println("order "+order);
            	order++;
                
                //featurizeString(splits[0]);
            	System.out.println(lineTxt);
                //Map<String, String> l = featurizeAbstract(lineTxt);
                Map<String, String> l = featurizeAbstractWithRegularRule(lineTxt);
                
        		System.out.println(l.get("branch"));
        		System.out.println(l.get("effect"));
                lineTxt = featureReader.readLine();
                //System.out.println("\n");
                //break;
            }
            read.close();
	    }else{
	        System.out.println("Error finding feature.rule");
	    }
	    } catch (Exception e) {
	        System.out.println("Error reading feature.rule");
	        e.printStackTrace();
	    }
	}
	
	private void TestFromFile(String filepos){
		int order = 1;
		try {
        File file=new File(filepos);
        if(file.isFile() && file.exists()){ //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
            new FileInputStream(file));//考虑到编码格式
            BufferedReader featureReader =  new BufferedReader(read);
            String lineTxt = null;
            while((lineTxt = featureReader.readLine()) != null){
                //System.out.println(lineTxt);
            	System.out.println("order "+order);
            	order++;
                
            	System.out.println(lineTxt);
                //Map<String, String> l = featurizeAbstract(lineTxt);
                Map<String, String> l = featurizeAbstractWithRegularRule(lineTxt);
        		System.out.println("branch: "+l.get("branch"));
        		System.out.println("effect: "+l.get("effect"));
            }
            read.close();
	    }else{
	        System.out.println("Error finding test input file");
	    }
	    } catch (Exception e) {
	        System.out.println("Test Exception");
	        e.printStackTrace();
	    }
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PatentFeaturize pf = PatentFeaturize.getInstance();
		//pf.TestDirectlyFromFile("testcase");
		pf.TestFromFile("磁存储");
		//pf.TestFromFile("testcase10");
		
		
	}

}
